//
//  AlipayAuthViewController.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/10.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class AlipayAuthViewController: BaseViewController,UITextFieldDelegate {

    var cashCount : String = ""
    var type = -1
    var field : UITextField?
    let accountCnt = UITextField.init()
    let nameCnt = UITextField.init()
    //针对微信平台
    var cash_account = ""
    var nick_nm = ""
    let stableLbl = UILabel.init()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.naviHide = false
        self.defaultBackButton()
        if type == 0 {
            self.title = "提现到支付宝"
        } else if type == 1 {
            self.title = "提现到微信"
        }
        self.view.backgroundColor = UIColor.colorWithHexString(hex: "f9f9f9")
        self.loadData()
        NotificationCenter.default.addObserver(self, selector: #selector(refreshBal), name: NSNotification.Name("refreshBalance"), object: nil)
        // Do any additional setup after loading the view.
    }
    
    @objc func refreshBal() -> Void {
        Manager.shared().getBalances(success: { (model) in
            if let mod = model as? LoginModel {
                let balance = NumberToString.intToFloatString(number: mod.blance, radix: 100)
                let txt = "账户余额" + balance + "元，" + "最低提现100.00元"
                self.stableLbl.text = txt
            }
        }, fail: {(code) in
            
        })
    }
    
    @objc func alipayWidth()->Void{
        self.view.endEditing(true)
        //构造参数
        var nick_name = ""
        if type == 0{
            //print(accountCnt)
            //print(nameCnt)
//            let result = NumberChecker.isMail(str: accountCnt.text) || NumberChecker.phoneNumCheck(phone: accountCnt.text)
            let result = NumberChecker.checkAlipayAccount(phone: accountCnt.text)
            if result == false {
                commonAlertMsg(content: "请输入合法的账户!")
                return
            }
            
            if nameCnt.text?.characters.count == 0 {
                commonAlertMsg(content: "请输入您的姓名!")
                return
            }
            nick_name = nameCnt.text!
        }
        
        if field!.text?.characters.count == 0 {
            self.commonAlertMsg(content: "请输入提现金额!")
            return
        }
        
        let id = DataCacher.shared().id()
        let str = String(id)
        let cash_money = NSString.init(string: field!.text!).floatValue
        let money = Int(cash_money * 100)
        if money < 10000 {
            self.commonAlertMsg(content: "提现金额必须大于等于100!")
            return
        }
        let hud = NetProgressHudView.init(supView: self.view, type: .Black)
        hud.beginRotate()
        var param : Dictionary<String,String> = [:]
        if type == 0 {
            param = ["bh_id":str,"cash_money":String(money),"cash_account":accountCnt.text!,"cash_type":String(type + 1),"nick_name":nick_name]
        } else {
            param = ["bh_id":str,"cash_money":String(money),"cash_account":cash_account,"cash_type":String(type + 1),"nick_name":nick_nm]
        }
        Manager.shared().widthDraw(param: param, success: {(mod) in
            hud.endRotate()
            if mod.result == true {
                if mod.result == true {
                    //刷新本地的余额
                    self.getMoneyAndRefresh()
                    let sure = EnSureAlipayViewController.init()
                    sure.amount = self.field!.text!
                    sure.type = self.type
                    if self.type == 0 {
                        sure.account = self.accountCnt.text!
                    } else if self.type == 1 {
                        sure.account = self.nick_nm
                    }
                    self.navigationController?.pushViewController(sure, animated: true)
                } else{
                    self.commonAlertMsg(content: mod.msg)
                }
            }else{
                self.commonAlertMsg(content: mod.msg)
            }
        }, fail: {(code) in
            hud.endRotate()
            self.commonAlertMsg(content: YXNetWorkFail)
        })
    }
    
    func getMoneyAndRefresh() -> Void {
        NotificationCenter.default.post(name: NSNotification.Name("refreshBalance"), object: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func loadData() -> Void {
        if type == 0{
            let tray1 = UIView.init()
            tray1.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 20 * YXHeightScale), size: CGSize.init(width: YXDeviceWidth, height: 88 * YXHeightScale))
            tray1.backgroundColor = UIColor.white
            self.view.addSubview(tray1)
            let nameTitle = UILabel.init()
            nameTitle.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 122 * YXWidthScale, height: 86 * YXHeightScale))
            nameTitle.textColor = UIColor.colorWithHexString(hex: "2c2c2c")
            nameTitle.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
            nameTitle.text = "姓名"
            nameTitle.textAlignment = .center
            tray1.addSubview(nameTitle)
            
            nameCnt.frame = CGRect.init(origin: CGPoint.init(x: nameTitle.right(), y: 0), size: CGSize.init(width: YXDeviceWidth - nameTitle.right(), height: 86 * YXHeightScale))
            nameCnt.delegate = self
            nameCnt.textColor = nameTitle.textColor
            nameCnt.font = nameTitle.font
            tray1.addSubview(nameCnt)
            let line = UIView.init()
            line.backgroundColor = UIColor.colorWithHexString(hex: "f2f2f2")
            line.frame = CGRect.init(origin: CGPoint.init(x: nameTitle.left(), y: nameTitle.bottom()), size: CGSize.init(width: 1000, height: YXHeightScale))
            tray1.addSubview(line)
            let tray2 = UIView.init()
            tray2.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 108 * YXHeightScale), size: CGSize.init(width: YXDeviceWidth, height: 88 * YXHeightScale))
            tray2.backgroundColor = UIColor.white
            self.view.addSubview(tray2)
            let accountTitle = UILabel.init()
            accountTitle.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 122 * YXWidthScale, height: 86 * YXHeightScale))
            accountTitle.textColor = UIColor.colorWithHexString(hex: "2c2c2c")
            accountTitle.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
            accountTitle.text = "账户"
            accountTitle.textAlignment = .center
            tray2.addSubview(accountTitle)
            
            accountCnt.frame = CGRect.init(origin: CGPoint.init(x: accountTitle.right(), y: 0), size: CGSize.init(width: YXDeviceWidth - nameTitle.right(), height: 86 * YXHeightScale))
            accountCnt.textColor = nameTitle.textColor
            accountCnt.font = nameTitle.font
            accountCnt.delegate = self
            tray2.addSubview(accountCnt)
            let line2 = UIView.init()
            line2.backgroundColor = UIColor.colorWithHexString(hex: "f2f2f2")
            line2.frame = CGRect.init(origin: CGPoint.init(x: nameTitle.left(), y: nameTitle.bottom()), size: CGSize.init(width: 1000, height: YXHeightScale))
            tray2.addSubview(line2)
        }
        
        let tray3 = UIView.init()
        if type == 0{
            tray3.frame = CGRect.init(x: 0, y: 238 * YXHeightScale, width: YXDeviceWidth, height: 288 * YXHeightScale)
        } else{
            tray3.frame = CGRect.init(x: 0, y: 20 * YXHeightScale, width: YXDeviceWidth, height: 288 * YXHeightScale)
        }
        tray3.backgroundColor = UIColor.white
        tray3.layer.borderColor = UIColor.colorWithHexString(hex: "f2f2f2").cgColor
        tray3.layer.borderWidth = YXHeightScale
        self.view.addSubview(tray3)
        
        let lbl = UILabel.init()
        let font = UIFont.systemFont(ofSize: 32 * YXWidthScale)
        let size = "提现金额".StringSize(font: font)
        lbl.frame = CGRect.init(x: 38 * YXWidthScale, y: 36 * YXHeightScale, width: size.width, height: size.height)
        lbl.textColor = UIColor.colorWithHexString(hex: "646464")
        lbl.font = font
        lbl.text = "提现金额"
        tray3.addSubview(lbl)
        
        let label = UILabel.init()
        label.textColor = UIColor.colorWithHexString(hex: "2c2c2c")
        label.text = "¥"
        let font1 = UIFont.systemFont(ofSize: 72 * YXWidthScale)
        let size1 = "¥".StringSize(font: font1)
        label.frame = CGRect.init(x: 38 * YXWidthScale, y: lbl.bottom() + 40 * YXHeightScale, width: size1.width, height: size1.height)
        label.font = font1
        tray3.addSubview(label)
        
        field = UITextField.init()
        field!.frame = CGRect.init(x: label.right() + 10 * YXWidthScale, y: label.top() - 10 * YXHeightScale, width: 1000, height: size1.height + 20 * YXHeightScale)
        field!.delegate = self
        field!.moneyPad()
        field!.font = font1
        tray3.addSubview(field!)
        
        let line1 = UIView.init()
        line1.frame = CGRect.init(x: 38 * YXWidthScale, y: 212 * YXHeightScale, width: 1000, height: YXHeightScale)
        line1.backgroundColor = UIColor.colorWithHexString(hex: "f2f2f2")
        tray3.addSubview(line1)
        
        refreshBal()
        stableLbl.textColor = UIColor.colorWithHexString(hex: "909090")
        stableLbl.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        stableLbl.frame = CGRect.init(x: line1.left(), y: line1.bottom(), width: 1000, height: 76 * YXHeightScale)
        tray3.addSubview(stableLbl)
        
        let button = UIButton.init(type: .system)
        button.layer.cornerRadius = YXButtonCorner
        button.backgroundColor = UIColor.black
        button.setTitle("确认提现", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 36 * YXWidthScale)
        button.setTitleColor(UIColor.white, for: .normal)
        button.frame = CGRect.init(x: (YXDeviceWidth - 674 * YXWidthScale) / 2, y: tray3.bottom() + 70 * YXHeightScale, width: 674 * YXWidthScale, height: 88 * YXHeightScale)
        button.addTarget(self, action: #selector(alipayWidth), for: .touchUpInside)
        self.view.addSubview(button)
    }
    
    
    //textfield delegate
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField == nameCnt {
            if (textField.text?.characters.count)! > 11 {
                if string == "" {
                    return true
                }
                return false
            }
            return true
        } else if textField == accountCnt {
            if (textField.text?.characters.count)! > 30 {
                if string == "" {
                    return true
                }
                return false
            }
            return true
        }
        return true
    }
    
    deinit {
        print("AlipayAuthViewController deinit")
    }

}
